package com.gitee.wsl.compose.chart.xy.bar

import androidx.compose.foundation.layout.absolutePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.gitee.wsl.common.ui.draw.style.LineStyle
import com.gitee.wsl.compose.chart.Chart
import com.gitee.wsl.compose.chart.ChartLayout
import com.gitee.wsl.compose.chart.ExperimentalKoalaPlotApi
import com.gitee.wsl.compose.chart.KoalaPlotTheme
import com.gitee.wsl.compose.chart.legend.Legend
import com.gitee.wsl.compose.chart.legend.LegendLocation
import com.gitee.wsl.compose.chart.legend.LegendSlate
import com.gitee.wsl.compose.chart.legend.defaultLegend
import com.gitee.wsl.compose.chart.xy.XYGraph
import com.gitee.wsl.compose.chart.xy.XYGraphScope
import com.gitee.wsl.compose.chart.xy.axis.AxisLabel
import com.gitee.wsl.compose.chart.xy.axis.AxisModel
import com.gitee.wsl.compose.chart.xy.axis.style.AxisStyle
import com.gitee.wsl.compose.chart.xy.axis.style.rememberAxisStyle
import com.gitee.wsl.compose.chart.xy.bar.model.BarEntry
import com.gitee.wsl.compose.chart.xy.line.Line
import com.gitee.wsl.compose.chart.xy.line.LinePlot
import com.gitee.wsl.compose.chart.xy.line.LinePlotSlate
import com.gitee.wsl.compose.chart.xy.model.PointEntry
import com.gitee.wsl.compose.modifier.input.gestures.GestureConfig

typealias BarComposable = @Composable BarScope.() -> Unit

typealias BarSlate = @Composable BarScope.(index: Int) -> Unit
object Bar

fun Bar.createBar(
    bar: BarComposable
): BarSlate = { bar() }

fun Bar.createBar(
    bar: StackedBarSlate
): BarSlate = { bar(it,0) }


typealias StackedBarSlate = @Composable BarScope.(xIndex: Int, barIndex: Int) -> Unit
object StackedBar

typealias BarPlotSlate<X, Y> =  @Composable XYGraphScope<X, Y>.(Int,List<BarEntry<X, Y>>) -> Unit
object BarPlot


@OptIn(ExperimentalKoalaPlotApi::class)
@Composable
fun <X,Y> Chart.VerticalBar(
    modifier: Modifier = Modifier,
    title: @Composable () -> Unit = {},
    legend: LegendSlate = Legend.defaultLegend,
    legendLocation: LegendLocation = KoalaPlotTheme.legendLocation,

    xAxisModel: AxisModel<X>,
    yAxisModel: AxisModel<Y>,
    xAxisStyle: AxisStyle = rememberAxisStyle(),
    xAxisLabels: @Composable (X) -> Unit = {
        AxisLabel(it.toString(), Modifier.padding(top = 2.dp))
    },
    xAxisTitle: @Composable () -> Unit = {},
    yAxisStyle: AxisStyle = rememberAxisStyle(),
    yAxisLabels: @Composable (Y) -> Unit ={
        AxisLabel(it.toString(), Modifier.absolutePadding(right = 2.dp))
    },
    yAxisTitle: @Composable () -> Unit = {},
    horizontalMajorGridLineStyle: LineStyle? = KoalaPlotTheme.axisThemeStyle.majorGridlineStyle,
    horizontalMinorGridLineStyle: LineStyle? = KoalaPlotTheme.axisThemeStyle.minorGridlineStyle,
    verticalMajorGridLineStyle: LineStyle? = KoalaPlotTheme.axisThemeStyle.majorGridlineStyle,
    verticalMinorGridLineStyle: LineStyle? = KoalaPlotTheme.axisThemeStyle.minorGridlineStyle,
    gestureConfig: GestureConfig = GestureConfig(),
    datas: List<BarEntry<X, Y>>,
    barPlotSlate: BarPlotSlate<X, Y> = BarPlot.VerticalBar()
){
    ChartLayout(
        modifier = modifier,
        title = title,
        legend = legend,
        legendLocation = legendLocation
    ) {
        XYGraph(
            xAxisModel = xAxisModel,
            yAxisModel = yAxisModel,
            xAxisStyle = xAxisStyle,
            xAxisLabels = xAxisLabels,
            xAxisTitle = xAxisTitle,
            yAxisStyle = yAxisStyle,
            yAxisLabels = yAxisLabels,
            yAxisTitle = yAxisTitle,
            horizontalMajorGridLineStyle = horizontalMajorGridLineStyle,
            horizontalMinorGridLineStyle = horizontalMinorGridLineStyle,
            verticalMajorGridLineStyle =verticalMajorGridLineStyle,
            verticalMinorGridLineStyle = verticalMinorGridLineStyle,
            gestureConfig = gestureConfig,
        ){
            barPlotSlate(0,datas)
        }
    }
}
